from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from database.database import get_db
from crud import user
from schemas.user import UserCreate, UserOut, UserRegister, UserLogin, UserLoginResponse
from typing import List

router = APIRouter()

# 用户注册
@router.post("/register", response_model=dict)
def register_user(user_data: UserRegister, db: Session = Depends(get_db)):
    """用户注册"""
    return user.register_user(db, user_data)

# 用户登录
@router.post("/login", response_model=UserLoginResponse)
def login_user(user_data: UserLogin, db: Session = Depends(get_db)):
    """用户登录"""
    db_user = user.login_user(db, user_data)
    return UserLoginResponse(
        id=db_user.id,
        username=db_user.username,
        phone=db_user.phone,
        email=db_user.email
    )

# 创建用户
@router.post("/", response_model=UserOut)
def create_user(user_data: UserCreate, db: Session = Depends(get_db)):
    """创建新用户"""
    return user.create_user(db, user_data)

# 获取指定用户
@router.get("/{user_id}", response_model=UserOut)
def get_user(user_id: int, db: Session = Depends(get_db)):
    """获取指定用户信息"""
    return user.get_user(db, user_id)

# 获取用户列表
@router.get("/", response_model=List[UserOut])
def get_users(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
    """获取用户列表"""
    return user.get_users(db, skip, limit)

# 更新用户
@router.put("/{user_id}", response_model=UserOut)
def update_user(user_id: int, phone: str = None, email: str = None, db: Session = Depends(get_db)):
    """更新用户信息"""
    return user.update_user(db, user_id, phone, email)

# 删除用户
@router.delete("/{user_id}")
def delete_user(user_id: int, db: Session = Depends(get_db)):
    """删除用户"""
    return user.delete_user(db, user_id) 